import SaveManager from "./Managers/SaveManager";


export function randomRange(min: number, max: number): number {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
export function unitConversion2(num: number): string {
    let value: number = num;
    let str: string = `${value}`;

    if (value > 999999999999) {
        str = this.keepOneDecimalFull2(value / 1000000000000) + "万亿";
    } else if (value > 99999999) {
        str = this.keepOneDecimalFull2(value / 100000000) + "亿";
    } else if (value > 99999) {
        str = this.keepOneDecimalFull(value / 10000) + "万";
    }

    return str;
}
//保留一位小数
export function keepOneDecimalFull(num: any): string {
    let result = parseFloat(num);
    if (isNaN(result)) {
        return "";
    }

    result = Math.floor(num * 10) / 10;
    let s_x = result.toString();
    let pos_decimal = s_x.indexOf(".");

    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += ".";
    }
    while (s_x.length <= pos_decimal + 1) {
        s_x += "0";
    }
    return s_x;
}

export function loadjsScript(url: string, cb: any) {
    let script = document.createElement("script");
    script.src = url;
    script.type = "text/javascript";
    script.onload = cb;
    document.getElementsByTagName("head")[0].appendChild(script);

}

export function getNetParams(obj: any, isRoleId: boolean = false): string {
    let params: string = isRoleId ? `roleId=${SaveManager.single.roleId}&` : "";
    if (obj != null) {
        for (const key in obj) {
            params += `${key}=` + obj[key] + "&";
        }
    }
    return params.substring(0, params.length - 1);
}

/**
     * 获取url 参数
     * @param {*} variable 
     */
export function getQueryVariable(variable, query: string) {


    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return "";
}

export function getTimehms(leftTime: number): string {

    if (leftTime <= 0) return "00:00";

    let str: string = "";
    let h = Math.floor(leftTime / 3600);
    if (h > 0) {

        h < 10 ? (str += "0" + h + ":") : (str += h + ":");
    }

    let m = h ? Math.floor(leftTime % 3600 / 60) : Math.floor(leftTime / 60);

    m < 10 ? (str += "0" + m) : (str += "" + m);

    let s = Math.floor(leftTime % 60);

    s < 10 ? (str += ":0" + s) : (str += ":" + s);

    return str;
}
/**获取0点时间戳 */
export function getZeroTime(): number {
    return new Date(new Date().toLocaleDateString()).getTime();
}